﻿using System;
using System.Net;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AR.Entities;
using AR.LoginUI;
using AR.Model;
using AR.ViewModels;
using AR.Web;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace AR.ViewModel
{
    public class LoginPageViewModel : GalaSoft.MvvmLight.ViewModelBase
    {

        private LoginInfo _loginInfo;
        public LoginInfo LoginInfo
        {
            get
            {
                return _loginInfo;
            }
            set
            {
                _loginInfo = value;
                RaisePropertyChanged("LoginInfo");
            }
        }


        private bool _rememberMe;
             public bool RememberMe
        {
            get
            {
                return _rememberMe;
            }
            set
            {
                _rememberMe = value;
                RaisePropertyChanged("RememberMe");
            }
        }

       
        /// <summary>
        /// The UserName property
        /// </summary>
        private string _userName;
        public string Username
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
                RaisePropertyChanged("Username");
            }
        }

        private Visibility _errLoginVisibility;
             public Visibility ErrLoginVisibility
        {
            get
            {
                return _errLoginVisibility;
            }
            set
            {
                _errLoginVisibility = value;
                RaisePropertyChanged("ErrLoginVisibility");
            }
        }
        
        private Visibility _invalidCodeVisible;
        public Visibility InvalidCodeVisible
        {
            get
            {
                return _invalidCodeVisible;
            }
            set
            {
                _invalidCodeVisible = value;
                RaisePropertyChanged("InvalidCodeVisible");
            }
        }
        


        private string _password;
        /// <summary>
        /// The Password property
        /// </summary>
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }
 
      private readonly CandidatModel model;
        
        
        public LoginPageViewModel()
        {
            if (!WebContext.Current.User.IsAuthenticated)
            {
                LoginInfo = new LoginInfo();
                model = new CandidatModel();
                InvalidCodeVisible = Visibility.Collapsed;
                ErrLoginVisibility = Visibility.Collapsed;
            }
            else 
            {
                NavigationService.Navigate("/ListeCandidats");
            }
        }      

        public ICommand PasserEntretienCommand
        {
            get
            {

                return new RelayCommand<object>((obj)=>
                {
                    this.PasserEntretien(obj);

                });
            }
        }

      
        public ICommand SeConnecterCommand
        {
            get
            {

                return new RelayCommand(()=>
                {
                    this.SeConnecter();

                });
            }
        }

        private void SeConnecter()
        {
            (Application.Current.RootVisual as BusyIndicator).IsBusy = true;
            AuthenticationContext authentifContext = new AuthenticationContext();
            LoginInfo.UserName = Username;
            LoginInfo.PasswordAccessor = () => Password;
            LoginInfo.RememberMe = RememberMe;
            //LoginInfo.RememberMe = re
            var loginOp = WebContext.Current.Authentication.Login(LoginInfo.ToLoginParameters());
            loginOp.Completed += (send, args)
                =>
            {
                // navigate again: this will re-check the credentials and continue to the 
                // correct page if you're logged in / got the required role

                if (WebContext.Current.User.IsAuthenticated)
                {
                   // if (Application.Current.RootVisual is MainPage)
                          // ((MainPage)(Application.Current.RootVisual)).ContentFrame.Navigate(new Uri("/ListeCandidats", UriKind.Relative));
                           //MainPage p = ((BusyIndicator)(Application.Current.RootVisual)).Content as MainPage;
                           //p.ContentFrame.Navigate(new Uri("/ListeCandidats", UriKind.Relative));

                           NavigationService.Navigate("/ListeCandidats");
                           
                }
                else
                    ErrLoginVisibility = Visibility.Visible;
                   (Application.Current.RootVisual as BusyIndicator).IsBusy = false;

            };
            //MessageBox.Show("sdfsdfd");
        }
        private void PasserEntretien(object obj)
        {
            model.isCandidatCodeValid(obj.ToString());
            CandidatModel.SetDelegate(NavigateToEntTechnique);
            
        }


        public void NavigateToEntTechnique(bool iscodevalid, CANDIDAT candidat)
        {
            if (iscodevalid && candidat.NOTE_TEST == null)
            {
               
                NavigationService.Navigate("/EntretienTechniqueForm/" + candidat.CODE_TEST);

            }
            else
            {
                InvalidCodeVisible = Visibility.Visible;
            }
        }
    }
}
